package org.dizem.groupon.service.impl;

import org.apache.log4j.Logger;
import org.dizem.groupon.dao.*;
import org.dizem.groupon.model.domain.ImageEntity;
import org.dizem.groupon.model.domain.ItemEntity;
import org.dizem.groupon.service.ItemService;
import org.hibernate.Hibernate;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * User: dizem@126.com
 * Time: 11-6-28 下午12:53
 */
public class ItemServiceImpl implements ItemService {
	private Logger log = Logger.getLogger(ItemServiceImpl.class);
	private ItemDAO itemDAO;
	private CityDAO cityDAO;
	private MerchantDAO merchantDAO;
	private ImageDAO imageDAO;
	private CategoryDAO categoryDAO;

	public CategoryDAO getCategoryDAO() {
		return categoryDAO;
	}

	public void setCategoryDAO(CategoryDAO categoryDAO) {
		this.categoryDAO = categoryDAO;
	}

	public ImageDAO getImageDAO() {
		return imageDAO;
	}

	public void setImageDAO(ImageDAO imageDAO) {
		this.imageDAO = imageDAO;
	}

	public ItemDAO getItemDAO() {
		return itemDAO;
	}

	public void setItemDAO(ItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}

	public CityDAO getCityDAO() {
		return cityDAO;
	}

	public void setCityDAO(CityDAO cityDAO) {
		this.cityDAO = cityDAO;
	}

	public MerchantDAO getMerchantDAO() {
		return merchantDAO;
	}

	public void setMerchantDAO(MerchantDAO merchantDAO) {
		this.merchantDAO = merchantDAO;
	}

	public void save(ItemEntity object) {
		itemDAO.save(object);
	}

	public void delete(ItemEntity object) {
		itemDAO.delete(object);
	}

	public void update(ItemEntity object) {
		itemDAO.update(object);
	}

	public ItemEntity getById(int id) {
		return itemDAO.getById(id);
	}

	public List<ItemEntity> loadAll() {
		return itemDAO.loadAll();
	}

	public List<ItemEntity> load(int firstResult, int maxResult) {
		return itemDAO.load(firstResult, maxResult);
	}

	public int getCount() {
		return itemDAO.getCount();
	}

	public void save(ItemEntity item, File[] images, String categoryName, String merchantName, String cityName) {
		item.setCity(cityDAO.getByChnName(cityName));
		item.setMerchant(merchantDAO.getByName(merchantName));
		item.setCategory(categoryDAO.getByName(categoryName));

		for (int i = 0; i < images.length; ++i) {
			try {
				ImageEntity img = new ImageEntity();
				FileInputStream str = new FileInputStream(images[i]);
				img.setImage(Hibernate.createBlob(str));
				imageDAO.save(img);

				switch (i) {
					case 0:
						item.setImage1(img);
						break;
					case 1:
						item.setImage2(img);
						break;
					case 2:
						item.setImage3(img);
						break;
				}
				log.info("saved img" + i + ": id = " + img.getId());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		save(item);
		log.info("saved item: " + item);
	}

	public List<ItemEntity> loadPastList(int firstResult, int maxResult) {
		return itemDAO.loadPastList(firstResult, maxResult);
	}

	public List<ItemEntity> loadCurrentList(int firstResult, int maxResult) {
		return itemDAO.loadCurrentList(firstResult, maxResult);
	}


	public int getPastCount() {
		return itemDAO.getPastCount();
	}

	public int getCurrentCount() {
		return itemDAO.getCurrentCount();
	}
}
